﻿Imports System.Xml
Imports System.IO
Imports System.Xml.Serialization
Imports System.ComponentModel
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Reflection
Imports System.Windows.Forms
Public Class AppSettings
#Region "EVENTS_DELEGATES"
    Implements ICloneable
    Public Event SQLExceptionHasChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event ErrorReportOnHasChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event UnHandledOnHasChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event WriteToErrorFolderHasChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event ErroTimeOutChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event SystemMessageVerboseHasChanged(ByVal sender As Object, ByVal e As appSettingEventArgs)
    Public Event OnError(ByVal sender As Object, ByVal e As appSettingEventArgs)
#End Region

#Region "PROPERTIES"
    Private _settingFile As String
    Public Property SettingFile() As String
        Get
            Return _settingFile
        End Get
        Set(ByVal value As String)
            _settingFile = value
        End Set
    End Property
    Private _ConnectionString1 As String
    Public Property ConnectionString1() As String
        Get
            Return _ConnectionString1
        End Get
        Set(ByVal value As String)
            _ConnectionString1 = value
        End Set
    End Property
    Private _ConnectionString2 As String
    Public Property ConnectionString2() As String
        Get
            Return _ConnectionString2
        End Get
        Set(ByVal value As String)
            _ConnectionString2 = value
        End Set
    End Property
    Private _ConnectionString3 As String
    Public Property ConnectionString3() As String
        Get
            Return _ConnectionString3
        End Get
        Set(ByVal value As String)
            _ConnectionString3 = value
        End Set
    End Property
    Private _errorConnectionString As String
    Public Property ErrorConnectionString() As String
        Get
            Return _errorConnectionString
        End Get
        Set(ByVal value As String)
            _errorConnectionString = value
        End Set
    End Property
    Private _ErrorTextFileDirectory As String
    Public Property ErrorTextFileDirectory() As String
        Get
            Return _ErrorTextFileDirectory
        End Get
        Set(ByVal value As String)
            _ErrorTextFileDirectory = value
        End Set
    End Property
    Private _verticalSplit As Integer
    Public Property VerticalSplit() As Integer
        Get
            Return _verticalSplit
        End Get
        Set(ByVal value As Integer)
            _verticalSplit = value
        End Set
    End Property
    Private _horizontalSplit As Integer
    Public Property HorizontalSplit() As Integer
        Get
            Return _horizontalSplit
        End Get
        Set(ByVal value As Integer)
            _horizontalSplit = value
        End Set
    End Property
    Private _errorConTimeOuit As Integer
    Public Property ErrorConTimeOut() As Integer
        Get
            Return _errorConTimeOuit
        End Get
        Set(ByVal value As Integer)
            If value > 100 Then
                value = 100
            End If
            If value < 5 Then
                value = 5
            End If
            _errorConTimeOuit = value
        End Set
    End Property
    Private _connectionTimeOut As Integer
    Public Property ConnectionTimeOut() As Integer
        Get
            Return _connectionTimeOut
        End Get
        Set(ByVal value As Integer)
            If value > 100 Then
                value = 100
            End If
            If value < 5 Then
                value = 5
            End If
            _connectionTimeOut = value
        End Set
    End Property
    Private _systemMessageMaxSize As Integer
    Public Property SystemMessageMaxSize() As Integer
        Get
            If _systemMessageMaxSize < 2000 Then
                _systemMessageMaxSize = 2000
            End If
            Return _systemMessageMaxSize
        End Get
        Set(ByVal value As Integer)
            _systemMessageMaxSize = value
        End Set
    End Property
    Private _systemMessageVerbose As Boolean
    Public Property SystemMessageVerbose() As Boolean
        Get
            Return _systemMessageVerbose
        End Get
        Set(ByVal value As Boolean)
            _systemMessageVerbose = value
        End Set
    End Property
    Private _addMessageDateTime As Boolean
    Public Property AddMessageDateTime() As Boolean
        Get
            Return _addMessageDateTime
        End Get
        Set(ByVal value As Boolean)
            _addMessageDateTime = value
        End Set
    End Property
    Private _errorReportingIsOn As Boolean
    Public Property ErrorReportingIsOn() As Boolean
        Get
            Return _errorReportingIsOn
        End Get
        Set(ByVal value As Boolean)
            _errorReportingIsOn = value
        End Set
    End Property
    Private _postSqlException As Boolean
    Public Property PostSqlException() As Boolean
        Get
            Return _postSqlException
        End Get
        Set(ByVal value As Boolean)
            _postSqlException = value
        End Set
    End Property
    Private _showSqlExceptionMessage As Boolean
    Public Property ShowSQLExceptionMessage() As Boolean
        Get
            Return _showSqlExceptionMessage
        End Get
        Set(ByVal value As Boolean)
            _showSqlExceptionMessage = value
        End Set
    End Property
    Private _unhandledExceptionIsOn As Boolean
    Public Property UnHandledExceptionIsOn() As Boolean
        Get
            Return _unhandledExceptionIsOn
        End Get
        Set(ByVal value As Boolean)
            _unhandledExceptionIsOn = value
        End Set
    End Property
    Private _writeErrorToFolder As Boolean
    Public Property WriteErrorToFolder() As Boolean
        Get
            Return _writeErrorToFolder
        End Get
        Set(ByVal value As Boolean)
            _writeErrorToFolder = value
        End Set
    End Property
    Private _errorVerBose As Boolean
    Public Property ErrorVerbose() As Boolean
        Get
            Return _errorVerBose
        End Get
        Set(ByVal value As Boolean)
            _errorVerBose = value
        End Set
    End Property
    Private _hideLeftPanelOnStartup As Boolean
    Public Property HideLeftPanelOnStartup() As Boolean
        Get
            Return _hideLeftPanelOnStartup
        End Get
        Set(ByVal value As Boolean)
            _hideLeftPanelOnStartup = value
        End Set
    End Property
    Private _hideSystemPanelOnStartup As Boolean
    Public Property HideSystemPanelOnStartup() As Boolean
        Get
            Return _hideSystemPanelOnStartup
        End Get
        Set(ByVal value As Boolean)
            _hideSystemPanelOnStartup = value
        End Set
    End Property
    Private _leftPanelVisible As Boolean
    Public Property LeftPAnelVisible() As Boolean
        Get
            Return _leftPanelVisible
        End Get
        Set(ByVal value As Boolean)
            _leftPanelVisible = value
        End Set
    End Property
    Private _systemPanelVisible As Boolean
    Public Property SystemPAnelVisible() As Boolean
        Get
            Return _systemPanelVisible
        End Get
        Set(ByVal value As Boolean)
            _systemPanelVisible = value
        End Set
    End Property
    Private _testErrorConOnStartUp As Boolean
    Public Property TestErrorConOnStartUp() As Boolean
        Get
            Return _testErrorConOnStartUp
        End Get
        Set(ByVal value As Boolean)
            _testErrorConOnStartUp = value
        End Set
    End Property

#End Region

#Region "RAISE_TO_ERRORMANAGER"
    Public Sub ErroTimeOutHasChanged()
        RaiseEvent ErroTimeOutChanged(Me, New appSettingEventArgs(_errorConTimeOuit))
    End Sub
    Public Sub WriteToErrorFolderChanged()
        RaiseEvent WriteToErrorFolderHasChanged(Me, New appSettingEventArgs(_writeErrorToFolder))
    End Sub
    Public Sub UnHandledOnChanged()
        RaiseEvent UnHandledOnHasChanged(Me, New appSettingEventArgs(_unhandledExceptionIsOn))
    End Sub
    Public Sub SQLExceptionChanged()
        RaiseEvent SQLExceptionHasChanged(Me, New appSettingEventArgs(_showSqlExceptionMessage))
    End Sub
    Public Sub ErrorReportOnChanged()
        RaiseEvent ErrorReportOnHasChanged(Me, New appSettingEventArgs(_errorReportingIsOn))
    End Sub
#End Region

#Region "UPDATE_UI"
    Public Sub SystemMessageVerboseChanged()
        RaiseEvent SystemMessageVerboseHasChanged(Me, New appSettingEventArgs(_systemMessageVerbose))
    End Sub
#End Region

#Region "CONSTRUCTORS"
    Private Sub New()
        Common.SystemMessage() = String.Empty
        _ConnectionString1 = String.Empty
        _ConnectionString2 = String.Empty
        _ConnectionString3 = String.Empty
        _errorConnectionString = String.Empty
        _ErrorTextFileDirectory = String.Empty
    End Sub
    Private Shared _instance As AppSettings
    Public Shared ReadOnly Property Instance() As AppSettings
        Get
            If IsNothing(_instance) Then
                _instance = New AppSettings
            End If
            Return _instance
        End Get
    End Property
    Public Function Clone() As Object Implements ICloneable.Clone
        Dim newGSettings As AppSettings = DirectCast(MemberwiseClone(), AppSettings)
        Return newGSettings
    End Function
    Public Sub setCurrent(ByVal A As AppSettings)
        _instance = A
    End Sub
    Private Sub loadDefaults()
        Common.SystemMessage() = "General Settings loaded from defaults."
        _ConnectionString1 = ""
        _ConnectionString2 = ""
        _ConnectionString3 = ""
        _errorConnectionString = ""
        _ErrorTextFileDirectory = "C:\"
        _settingFile = String.Empty
        _verticalSplit = 210
        _horizontalSplit = 400
        _errorConTimeOuit = 5
        _connectionTimeOut = 5
        _errorReportingIsOn = True
        _showSqlExceptionMessage = False
        _unhandledExceptionIsOn = True
        _writeErrorToFolder = False
        _errorVerBose = False
        _hideSystemPanelOnStartup = False
        _hideLeftPanelOnStartup = False
        _systemPanelVisible = True
        _leftPanelVisible = True
        _systemMessageVerbose = True
        _testErrorConOnStartUp = False
    End Sub
#End Region

#Region "XML_WRITE_READ"
    Public Sub WriteSettingToXml(ByVal filename As String)
        Dim objStreamWriter As New StreamWriter(filename)
        Dim rec As AppSettings = AppSettings.Instance
        Try
            Dim x As New XmlSerializer(rec.GetType)
            x.Serialize(objStreamWriter, rec)
        Catch ex As Exception
            Throw ex
        Finally
            objStreamWriter.Close()
        End Try
    End Sub
    Public Function ReadSettingsFromXML(ByVal filename As String) As Boolean
        Dim result As Boolean
        Dim errorException As Exception = Nothing
        Dim objStreamReader As StreamReader = Nothing
        If Not String.IsNullOrEmpty(filename) Then
            Dim io As New IO.FileInfo(filename)
            If io.Exists Then
                Try
                    Dim rec As AppSettings = AppSettings.Instance
                    objStreamReader = New StreamReader(filename)
                    Dim x As New XmlSerializer(rec.GetType)
                    rec = CType(x.Deserialize(objStreamReader), AppSettings)
                    result = ReadappSettings(rec)
                Catch ex As Exception
                    errorException = ex
                    result = False
                Finally
                    If Not IsNothing(objStreamReader) Then
                        objStreamReader.Close()
                    End If
                End Try
            Else
                errorException = New Exception("ReadappSettingsFromXML - " & filename & " was not found.")
            End If
        Else
            errorException = New Exception("ReadappSettingsFromXML handled - Empty String")
        End If
        If errorException IsNot Nothing Then
            loadDefaults()
            RaiseEvent OnError(Me, New appSettingEventArgs(errorException))
        End If
        _settingFile = filename
        Return result
    End Function
    Public Function ReadappSettings(ByVal rec As AppSettings) As Boolean
        Dim result As Boolean
        Dim errorException As Exception = Nothing
        If Not IsNothing(rec) Then
            Try
                Dim o As AppSettings = AppSettings.Instance
                Dim otype As Type = o.GetType
                Dim recType As Type = rec.GetType
                Dim oPropInfo() As PropertyInfo = otype.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
                Dim RecPropInfo() As PropertyInfo = recType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
                Dim index As Integer
                For Each p As PropertyInfo In oPropInfo
                    p.SetValue(o, RecPropInfo(index).GetValue(rec), Nothing)
                    index += 1
                Next
                result = True
            Catch ex As Exception
                errorException = ex
            End Try
        Else
            errorException = New Exception("ReadAppSettings - Empty rec for appsettings.")
        End If
        'If errorException IsNot Nothing Then
        '    Throw errorException
        'End If
        Return result
    End Function
#End Region

End Class
